﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TAMMODUL
    {
        public TAMMODUL()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VAMMODUL();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();
            dwData["AMMMANDANT"] = app.Mandant;
            dwData["AMMNRKRS"] = " AND AMMNRKRS LIKE'" + KRS + "'";
            dwData["AMMLFDNR"] = " AND AMMLFDNR='" + LFD + "'";
            dwData["AMMPOS"] = " AND AMMPOS='" + POS + "'";
            SResult = TMV.QAMD02(dwData);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {

                if (!IsNew)
                {
                    IsNew = true;
                    ART = "001";
                    UPOS = "1";
                    return;
                } 
                PM["MKRS"] = KRS;
                PM["MLFD"] = LFD;
                PM["MPOS"] = POS;
                PM["MNR"] = ART;
                PM["UPOS"] = UPOS;
                TMV.NEWMOD(PM);
                vwMaster.View.MoveCurrentToFirst();
                IsNew = false;
                ART = "1";
                UPOS = "1";
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DAMD01(dwMaster);
            filter = "AMMNRKRS='" + dwMaster["AMMNRKRS"].ToString() + "' AND AMMLFDNR='" + dwMaster["AMMLFDNR"].ToString() + "' AND AMMPOS='" + dwMaster["AMMPOS"].ToString()
                + "' AND AMMMODNR='" + dwMaster["AMMMODNR"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.UpdateAMMODUL(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
           
            SResult = TMV.QAMD03(dwMaster);
            foreach (DataRow dwAM in SResult.Tables[0].Rows)
            {
                dwData = TBMain.NewRow();
                dwData["AMMMANDANT"] = app.Mandant;
                dwData["AMMNRKRS"] = dwAM["AMNRKRS"];
                dwData["AMMLFDNR"] = dwAM["AMLFDNR"];
                dwData["AMMPOS"] = dwAM["AMPOS"];
                TMV.DAMD05(dwData);
            }
            foreach (DataRow row in TBMain.Copy().Rows)
            {
                foreach(DataRow dwAM in SResult.Tables[0].Rows )
                {                    
                    row["AMMNRKRS"] = dwAM["AMNRKRS"];
                    row["AMMLFDNR"] = dwAM["AMLFDNR"];
                    row["AMMPOS"] = dwAM["AMPOS"];
                    TMV.InsertAMMODUL(row);
                }
            }
            base.CopyCommand(sender, e);
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            /*

            DataSet Set = new DataSet();
            cDR.DocType = PRTYPE;
            Set = INTEXBL.QueryVKK(app.Mandant, dwEKK["VKKAUFKRS"].ToString(), dwEKK["VKKAUFLFD"].ToString());
            dwData = VSDDAT.SD22.TBMain.NewRow();
            dwData["SDSA"] = "0013";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0114";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0208";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0734";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));

            dwData["SDSA"] = "0241";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));

            dwData["SDSA"] = "0477";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            Set.Merge(INTEXDBC.QueryHRSTAMM(dwData, 3));
            cDR.PRINT(Set);*/
        }


        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != KEY)
                return;
            if (KEY.Length < 11)
                return;
            dwData = TMV.TBMain.NewRow();

            dwData["AMMMANDANT"] = app.Mandant;
            dwData["AMMNRKRS"] = "AND AMMNRKRS= '" + KEY.Substring(0, 2) + "'";
            dwData["AMMLFDNR"] = "AND AMMLFDNR= '" + KEY.Substring(2, 4) + "'";
            dwData["AMMPOS"] = "AND AMMPOS= '" + KEY.Substring(6, 2) + "'";
            dwData["AMMMODNR"] = "AND AMMMODNR= '" + KEY.Substring(8, 3) + "'";
            SResult = TMV.QAMD02(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 8)
                return;
            dwData = TMV.TBMain.NewRow();
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 4);
            POS = EXAUF.Substring(6, 2);
            dwData["AMMMANDANT"] = app.Mandant;
            dwData["AMMNRKRS"] = "AND AMMNRKRS= '" + EXAUF.Substring(0, 2) + "'";
            dwData["AMMLFDNR"] = "AND AMMLFDNR= '" + EXAUF.Substring(2, 4) + "'";
            dwData["AMMPOS"] = "AND AMMPOS= '" + EXAUF.Substring(6, 2) + "'";
            SResult = TMV.QAMD02(dwData);
            MergeData(SResult, true);
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count > 0)
                return;
            PM["MKRS"] = KRS;
            PM["MLFD"] = LFD;
            PM["MPOS"] = POS;
            PM["MNR"] = "010";
            PM["UPOS"] = "1";
            TMV.NEWMOD(PM);
            /*
            PM["MKRS"] = KRS;
            PM["MLFD"] = LFD;
            PM["MPOS"] = POS;
            PM["MNR"] = "999";
            PM["UPOS"] = "1";
            TMV.NEWMOD(PM);*/
            base.OnEXAUFChg(vlaue);
        }

    }
}
